package cn.lg.soar.boot.fc.service.impl;

import cn.lg.soar.boot.client.entity.Applicant;
import cn.lg.soar.boot.client.entity.ClientUser;
import cn.lg.soar.boot.client.service.ApplicantService;
import cn.lg.soar.boot.client.service.ClientUserService;
import cn.lg.soar.boot.fc.entity.*;
import cn.lg.soar.boot.fc.entity.Process;
import cn.lg.soar.boot.fc.service.*;
import cn.lg.soar.common.data.msg.I18n;
import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.util.AssertUtil;
import cn.lg.soar.common.util.ParameterUtil;
import cn.lg.soar.database.query.SqueryWapper;
import cn.lg.soar.system.auth.entity.Org;
import cn.lg.soar.system.auth.entity.User;
import cn.lg.soar.system.auth.entity.UserOrg;
import cn.lg.soar.system.auth.entity.UserRole;
import cn.lg.soar.system.auth.model.UserOrgItemVO;
import cn.lg.soar.system.auth.service.IOrgService;
import cn.lg.soar.system.auth.service.IUserService;
import cn.lg.soar.system.auth.service.UserOrgService;
import cn.lg.soar.system.auth.service.UserRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.lg.soar.boot.fc.mapper.ProcessExamineMapper;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 流程审批表
 * @author ZhuanZ（无密码）
 * @since 2025-01-15
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Service
public class ProcessExamineServiceImpl extends ServiceImpl<ProcessExamineMapper, ProcessExamine> implements ProcessExamineService {

    @Autowired
    protected IUserService userService;
    @Autowired
    protected ProcessService processService;
    @Autowired
    protected ProcessNodeService processNodeService;
    @Autowired
    private UserOrgService userOrgService;
    @Autowired
    protected IOrgService orgService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    protected ProcessApplyService processApplyService;
    @Autowired
    protected ProcessRecordService processRecordService;
    @Autowired
    protected TenancyRoomService tenancyRoomService;
    @Autowired
    protected RoomService roomService;
    @Autowired
    protected LeaseService leaseService;
    @Autowired
    protected FormCollectService formCollectService;
    /** 客户端用户信息服务 */
    @Autowired private ClientUserService clientUserService;
    @Autowired
    private ApplicantService applicantService;

    private SqueryWapper<ProcessExamine> examineWapper;
    private SqueryWapper<ProcessNode> nodeWapper;
    private SqueryWapper<UserRole> userRoleWapper;
    private SqueryWapper<TenancyRoom> tenancyRoomWapper;
    private SqueryWapper<FormCollect> collectWapper;

    /**
     * 用户申请流程审批
     */
    @Transactional
    public HttpResult<Void> examineApply(Long processApplyId, Long processId, Long applicantId, LocalDateTime now, Long customFormId, Long collectId,Long userId) {
//        User user = userService.getById(applicantId);
//        if (null == user){
//            return HttpResult.fail("B0000", I18n.build("申请用户为空！"));
//        }
        String applicantName = null;
        if(applicantId != null){
            Applicant applicant = applicantService.getById(applicantId);
            if(applicant != null) {
                applicantName =applicant.getName();
            }
        }
        ClientUser user = clientUserService.getById(userId);
        if(user == null) {
            return HttpResult.fail("B0000", I18n.build("申请用户为空！"));
        }
        Process process = processService.getById(processId);
        if(null == process){
            return HttpResult.fail("B0000", I18n.build("申请流程为空！"));
        }
        if (examineWapper == null) {
            examineWapper = new SqueryWapper<>(ProcessExamine.class);
        }
        QueryWrapper<ProcessExamine> examineWrapper = Wrappers.query();
        List<String> examineF = new ArrayList<>();
        examineF.add("processApplyId-eq~"+processApplyId);
//        examineF.add("applicantId-eq~"+applicantId);
        examineF.add("processId-eq~"+processId);
//        examineF.add("customFormId-eq~"+customFormId);
        examineF.add("isEnd-eq~0");
        examineWapper.setWhere(examineF, examineWrapper);
        List<ProcessExamine> list1 = super.list(examineWrapper);
        if(!list1.isEmpty()){
            return HttpResult.fail("B0000", I18n.build("用户申请流程已存在！"));
        }

        if (nodeWapper == null) {
            nodeWapper = new SqueryWapper<>(ProcessNode.class);
        }
        QueryWrapper<ProcessNode> nodeWrapper = Wrappers.query();
        List<String> nodeF = new ArrayList<>();
        nodeF.add("processId-eq~"+processId);
        List<String> orderBy = new ArrayList<>();
        orderBy.add("orders");
        nodeWapper.setWhere(nodeF, nodeWrapper);
        nodeWapper.setOrderBy(orderBy, nodeWrapper);
        List<ProcessNode> list = processNodeService.list(nodeWrapper);
        for (ProcessNode jwProcessNodeVO : list) {
            ProcessExamine jwProcessExamineEntity = new ProcessExamine();
            jwProcessExamineEntity.setProcessId(processId);
            jwProcessExamineEntity.setProcessName(process.getProcessName());
            jwProcessExamineEntity.setCollectId(collectId);
            jwProcessExamineEntity.setProcessApplyId(processApplyId);
            jwProcessExamineEntity.setCustomFormId(customFormId);
            jwProcessExamineEntity.setUserId(user.getId());
            jwProcessExamineEntity.setUserName(user.getUserName());
            jwProcessExamineEntity.setApplicantId(applicantId);
            jwProcessExamineEntity.setApplicantName(applicantName);
            jwProcessExamineEntity.setApplyTime(now);
            jwProcessExamineEntity.setProcessNodeId(jwProcessNodeVO.getId());
            jwProcessExamineEntity.setExamineType(jwProcessNodeVO.getExamineType());
            jwProcessExamineEntity.setNodeReject(jwProcessNodeVO.getNodeReject());
            jwProcessExamineEntity.setExamineMode(jwProcessNodeVO.getExamineMode());
            jwProcessExamineEntity.setIsEnd(0);
            if(jwProcessNodeVO.getOrders() > 0){
                jwProcessExamineEntity.setResult(3);
            }else{
                jwProcessExamineEntity.setResult(0);
            }
            jwProcessExamineEntity.setOrders(jwProcessNodeVO.getOrders());
            // 所在部门负责人审核添加
            if(jwProcessNodeVO.getExamineType() == 1){
                Org org = orgService.getById(user.getOrgId());
                if(org != null && org.getDirector() != null){
                    User approver = userService.getById(org.getDirector());
                    if(approver != null){
                        jwProcessExamineEntity.setId(null);
                        jwProcessExamineEntity.setApproverId(approver.getId());
                        jwProcessExamineEntity.setApproverName(approver.getNickname());
                        AssertUtil.isTrue(super.save(jwProcessExamineEntity), "创建失败");
                    }
                }

//                List<UserOrgItemVO> orgs = userOrgService.getOrgs(user.getId());
//                if(!orgs.isEmpty()){
//                    for(UserOrgItemVO orgItemVO : orgs){
//                        Org org = orgService.getById(orgItemVO.getOrgId());
//                        if(org != null && org.getDirector() != null){
//                            User approver = userService.getById(org.getDirector());
//                            if(approver != null){
//                                jwProcessExamineEntity.setId(null);
//                                jwProcessExamineEntity.setApproverId(approver.getId());
//                                jwProcessExamineEntity.setApproverName(approver.getNickname());
//                                AssertUtil.isTrue(super.save(jwProcessExamineEntity), "创建失败");
//                            }
//                        }
//                    }
//                }
            }
            // 所在部门连级负责人审核添加
            if(jwProcessNodeVO.getExamineType() == 3){
                int conOrders = 0;
                Org org = orgService.getById(user.getOrgId());
                if(org != null && org.getDirector() != null){
                    User approver = userService.getById(org.getDirector());
                    if(approver != null){
                        jwProcessExamineEntity.setId(null);
                        jwProcessExamineEntity.setApproverId(approver.getId());
                        jwProcessExamineEntity.setApproverName(approver.getNickname());
                        jwProcessExamineEntity.setConOrders(conOrders);
                        AssertUtil.isTrue(super.save(jwProcessExamineEntity), "创建失败");
                    }
                }
                while (true){
                    if(org.getParentId() != 0){
                        conOrders++;
                        org = orgService.getById(org.getParentId());
                        if(org != null && org.getDirector() != null){
                            User approver = userService.getById(org.getDirector());
                            if(approver != null){
                                jwProcessExamineEntity.setId(null);
                                jwProcessExamineEntity.setApproverId(approver.getId());
                                jwProcessExamineEntity.setApproverName(approver.getNickname());
                                jwProcessExamineEntity.setConOrders(conOrders);
                                if(conOrders > 0){
                                    jwProcessExamineEntity.setResult(3);
                                }
                                AssertUtil.isTrue(super.save(jwProcessExamineEntity), "创建失败");
                            }
                        }
                    }else{
                        break;
                    }
                }

//                List<UserOrgItemVO> orgs = userOrgService.getOrgs(user.getId());
//                if(!orgs.isEmpty()){
//                    for(UserOrgItemVO orgItemVO : orgs){
//                        int conOrders = 0;
//                        Org org = orgService.getById(orgItemVO.getOrgId());
//                        if(org != null && org.getDirector() != null){
//                            User approver = userService.getById(org.getDirector());
//                            if(approver != null){
//                                jwProcessExamineEntity.setId(null);
//                                jwProcessExamineEntity.setApproverId(approver.getId());
//                                jwProcessExamineEntity.setApproverName(approver.getNickname());
//                                jwProcessExamineEntity.setConOrders(conOrders);
//                                AssertUtil.isTrue(super.save(jwProcessExamineEntity), "创建失败");
//                            }
//                        }
//                        while (true){
//                            if(org.getParentId() != 0){
//                                conOrders++;
//                                org = orgService.getById(org.getParentId());
//                                if(org != null && org.getDirector() != null){
//                                    User approver = userService.getById(org.getDirector());
//                                    if(approver != null){
//                                        jwProcessExamineEntity.setId(null);
//                                        jwProcessExamineEntity.setApproverId(approver.getId());
//                                        jwProcessExamineEntity.setApproverName(approver.getNickname());
//                                        jwProcessExamineEntity.setConOrders(conOrders);
//                                        if(conOrders > 0){
//                                            jwProcessExamineEntity.setResult(3);
//                                        }
//                                        AssertUtil.isTrue(super.save(jwProcessExamineEntity), "创建失败");
//                                    }
//                                }
//                            }else{
//                                break;
//                            }
//                        }
//                    }
//                }
            }
            // 指定审核人审核添加
            if(jwProcessNodeVO.getApproverIds() != null){
                String[] approverIds = jwProcessNodeVO.getApproverIds().split(",");
                for (String approverId : approverIds) {
                    User approver = userService.getById(Long.parseLong(approverId));
                    if(approver != null){
                        jwProcessExamineEntity.setId(null);
                        jwProcessExamineEntity.setApproverId(approver.getId());
                        jwProcessExamineEntity.setApproverName(approver.getNickname());
                        AssertUtil.isTrue(super.save(jwProcessExamineEntity), "创建失败");
                    }
                }
            }
            // 角色审核添加
            if(jwProcessNodeVO.getRoleIds() != null){
                String roleIds = jwProcessNodeVO.getRoleIds().replaceAll(",","~");
//                String[] roleIds = jwProcessNodeVO.getRoleIds().split(",");

                if (userRoleWapper == null) {
                    userRoleWapper = new SqueryWapper<>(UserRole.class);
                }
                QueryWrapper<UserRole> userRoleWrapper = Wrappers.query();
                List<String> userRoleF = new ArrayList<>();
                userRoleF.add("roleId-in~"+roleIds);
                userRoleWapper.setWhere(userRoleF, userRoleWrapper);
                List<UserRole> userRolelist = userRoleService.list(userRoleWrapper);
                for (UserRole employeeVo : userRolelist) {
                    User approver = userService.getById(employeeVo.getUserId());
                    if(approver != null){
                        jwProcessExamineEntity.setId(null);
                        jwProcessExamineEntity.setApproverId(approver.getId());
                        jwProcessExamineEntity.setApproverName(approver.getNickname());
                        AssertUtil.isTrue(super.save(jwProcessExamineEntity), "创建失败");
                    }
                }
            }
        }
        return HttpResult.success("用户申请审批成功");
    }

    /**
     * 审批人审批流程
     */
    @Transactional
    public HttpResult<Void> processExamine(ProcessExamine updateForm) {
        if (examineWapper == null) {
            examineWapper = new SqueryWapper<>(ProcessExamine.class);
        }
        QueryWrapper<ProcessExamine> examineWrapper;

        LocalDateTime now = LocalDateTime.now();
        updateForm.setExamineTime(now);
        ParameterUtil.notNull(updateForm, "数据不能为空");
        AssertUtil.isTrue(super.updateById(updateForm), "更新失败");
        updateForm = super.getById(updateForm.getId());

        String operationContent = "";
        ProcessRecord addForm = new ProcessRecord();
        addForm.setProcessId(updateForm.getProcessId());
        addForm.setProcessNodeId(updateForm.getProcessNodeId());
        addForm.setProcessApplyId(updateForm.getProcessApplyId());
        addForm.setProcessExamineId(updateForm.getId());
        addForm.setUserId(updateForm.getUserId());
        addForm.setUserName(updateForm.getUserName());
        addForm.setApplicantId(updateForm.getApplicantId());
        addForm.setApplicantName(updateForm.getApplicantName());
        addForm.setApproverId(updateForm.getApproverId());
        addForm.setApproverName(updateForm.getApproverName());
        addForm.setReceiveTime(updateForm.getReceiveTime());
        addForm.setExamineTime(now);
        addForm.setOpinion(updateForm.getOpinion());
        // 通过
        if(updateForm.getResult() == 1){
            String content = "审批人["+updateForm.getApproverName()+"]在"+now+"审核通过了申请人["+updateForm.getApplicantName()+"]的申请;";
            addForm.setResult(1);

            Boolean isNext = true;
            if(updateForm.getExamineMode() == 0){
                // 或签：同一节点其他审批人状态改为或签通过
                examineWrapper = Wrappers.query();
                List<String> examineF = new ArrayList<>();
                examineF.add("processApplyId-eq~"+updateForm.getProcessApplyId());
//                examineF.add("applicantId-eq~"+updateForm.getApplicantId());
                examineF.add("processId-eq~"+updateForm.getProcessId());
//                examineF.add("customFormId-eq~"+updateForm.getCustomFormId());
                examineF.add("result-eq~0");
                examineF.add("isEnd-eq~0");
                examineF.add("orders-eq~"+updateForm.getOrders());
                examineWapper.setWhere(examineF, examineWrapper);
                List<ProcessExamine> list = super.list(examineWrapper);
                for (ProcessExamine examineVO : list) {
                    examineVO.setResult(2);
                    AssertUtil.isTrue(super.updateById(examineVO), "更新失败");
                }
                operationContent = content + "审核方式为或签,同一节点其他审核人审核状态将改为或签通过;审核将流转至下一节点;";
            }else{
                // 会签：同一节点所有审批人都审核通过后才流转下一节点 否则不做修改
                examineWrapper = Wrappers.query();
                List<String> examineF = new ArrayList<>();
                examineF.add("processApplyId-eq~"+updateForm.getProcessApplyId());
//                examineF.add("applicantId-eq~"+updateForm.getApplicantId());
                examineF.add("processId-eq~"+updateForm.getProcessId());
//                examineF.add("customFormId-eq~"+updateForm.getCustomFormId());
                examineF.add("isEnd-eq~0");
                examineF.add("result-ne~1");
                examineF.add("orders-eq~"+updateForm.getOrders());
                examineWapper.setWhere(examineF, examineWrapper);
                List<ProcessExamine> list = super.list(examineWrapper);
                if(!list.isEmpty()){
                    for (ProcessExamine examineVO : list) {
                        if(examineVO.getExamineType() == 3){
                            if(examineVO.getConOrders() == (updateForm.getConOrders()+1)){
                                isNext = false;
                                operationContent = content + "审批人为连级主管,审核将流转至下级主管;";
                                examineVO.setResult(0);
                                examineVO.setReceiveTime(now);
                                AssertUtil.isTrue(super.updateById(examineVO), "更新失败");
                                break;
                            }
                        }else{
                            isNext = false;
                            operationContent = content + "审核方式为会签,等待同一节点其他审核人审核通过;";
                            break;
                        }
                    }
                }else{
                    operationContent = content + "审核方式为会签,同一节点所有审核人都审核通过;审核将流转至下一节点;";
                }
            }

            if(isNext){
                // 流转至下一节点
                examineWrapper = Wrappers.query();
                List<String> examineF = new ArrayList<>();
                examineF.add("processApplyId-eq~"+updateForm.getProcessApplyId());
//                examineF.add("applicantId-eq~"+updateForm.getApplicantId());
                examineF.add("processId-eq~"+updateForm.getProcessId());
//                examineF.add("customFormId-eq~"+updateForm.getCustomFormId());
                examineF.add("isEnd-eq~0");
                examineF.add("orders-eq~"+ (updateForm.getOrders() + 1));
                System.out.println("============================================updateForm.getOrders()=========="+(updateForm.getOrders() + 1));
                examineWapper.setWhere(examineF, examineWrapper);
                List<ProcessExamine> list = super.list(examineWrapper);
                if(list.size() <= 0){
                    // 当前节点为最后一个节点 没下一节点时 所有节点流程结束
                    examineWrapper = Wrappers.query();
                    examineF = new ArrayList<>();
                    examineF.add("processApplyId-eq~"+updateForm.getProcessApplyId());
//                    examineF.add("applicantId-eq~"+updateForm.getApplicantId());
                    examineF.add("processId-eq~"+updateForm.getProcessId());
//                    examineF.add("customFormId-eq~"+updateForm.getCustomFormId());
                    examineF.add("isEnd-eq~0");
                    examineWapper.setWhere(examineF, examineWrapper);
                    list = super.list(examineWrapper);
                    for (ProcessExamine examineVO : list) {
                        examineVO.setIsEnd(1);
                        AssertUtil.isTrue(super.updateById(examineVO), "更新失败");
                    }
                    operationContent = content + "此节点为最后节点,此流程所有节点流程结束;";
                    // 用户申请审批状态改为通过
                    examine(updateForm.getProcessApplyId(), 1);
                }else{
                    // 下一节点审核状态从待流转改为待审批
                    for (ProcessExamine examineVO : list) {
                        examineVO.setResult(0);
                        examineVO.setReceiveTime(now);
                        AssertUtil.isTrue(super.updateById(examineVO), "更新失败");
                    }
                }
            }
        }else{ // 拒绝
            String content = "审批人["+updateForm.getApproverName()+"]在"+now+"审核拒绝了申请人["+updateForm.getApplicantName()+"]的申请;";
            addForm.setResult(9);
            List<String> examineF;
            List<ProcessExamine> list;
            if(updateForm.getNodeReject() == 0){
                // 直接结束整个流程
                examineWrapper = Wrappers.query();
                examineF = new ArrayList<>();
                examineF.add("processApplyId-eq~"+updateForm.getProcessApplyId());
//                examineF.add("applicantId-eq~"+updateForm.getApplicantId());
                examineF.add("processId-eq~"+updateForm.getProcessId());
//                examineF.add("customFormId-eq~"+updateForm.getCustomFormId());
                examineF.add("isEnd-eq~0");
                examineWapper.setWhere(examineF, examineWrapper);
                list = super.list(examineWrapper);
                for (ProcessExamine examineVO : list) {
                    examineVO.setIsEnd(1);
                    AssertUtil.isTrue(super.updateById(examineVO), "更新失败");
                }
                operationContent = content + "此节点设置为驳回时结束整个流程,此流程所有节点流程结束;";
                // 用户申请审批状态改为拒绝
                examine(updateForm.getProcessApplyId(), 9);
            }else if (updateForm.getNodeReject() == 1){
                // 驳回到前一个审批节点
                examineWrapper = Wrappers.query();
                examineF = new ArrayList<>();
                examineF.add("processApplyId-eq~"+updateForm.getProcessApplyId());
//                examineF.add("applicantId-eq~"+updateForm.getApplicantId());
                examineF.add("processId-eq~"+updateForm.getProcessId());
//                examineF.add("customFormId-eq~"+updateForm.getCustomFormId());
                examineF.add("isEnd-eq~0");
                // 审批人为连级主管时
                if(updateForm.getExamineType() == 3){
                    if(updateForm.getConOrders() == 0){
                        examineF.add("orders-eq~"+ (updateForm.getOrders() - 1));
                    }else {
                        examineF.add("orders-eq~"+ updateForm.getOrders());
                        examineF.add("conOrders-eq~"+ (updateForm.getConOrders() - 1));
                    }
                }else{
                    examineF.add("orders-eq~"+ (updateForm.getOrders() - 1));
                }
                examineWapper.setWhere(examineF, examineWrapper);
                list = super.list(examineWrapper);
                if(list.size() <= 0){
                    // 当此节点为第一个节点时 驳回时应当结束整个流程
                    examineWrapper = Wrappers.query();
                    examineF = new ArrayList<>();
                    examineF.add("processApplyId-eq~"+updateForm.getProcessApplyId());
//                    examineF.add("applicantId-eq~"+updateForm.getApplicantId());
                    examineF.add("processId-eq~"+updateForm.getProcessId());
//                    examineF.add("customFormId-eq~"+updateForm.getCustomFormId());
                    examineF.add("isEnd-eq~0");
                    examineWapper.setWhere(examineF, examineWrapper);
                    list = super.list(examineWrapper);
                    for (ProcessExamine examineVO : list) {
                        examineVO.setIsEnd(1);
                        AssertUtil.isTrue(super.updateById(examineVO), "更新失败");
                    }
                    operationContent = content + "此节点设置为驳回到前一个审批节点,当前节点为第一个节点,此流程所有节点流程结束;";
                    // 用户申请审批状态改为拒绝
                    examine(updateForm.getProcessApplyId(), 9);
                }else{
                    // 将前一个节点状态改为待审批
                    for (ProcessExamine examineVO : list) {
                        examineVO.setResult(0);
                        examineVO.setReceiveTime(now);
                        AssertUtil.isTrue(super.updateById(examineVO), "更新失败");
                    }
                    operationContent = content + "此节点设置为驳回到前一个审批节点,审核将流转至上一节点;";
                }
            }else if (updateForm.getNodeReject() == 2){
                // 继续流转至下一个审批节点
                examineWrapper = Wrappers.query();
                examineF = new ArrayList<>();
                examineF.add("processApplyId-eq~"+updateForm.getProcessApplyId());
//                examineF.add("applicantId-eq~"+updateForm.getApplicantId());
                examineF.add("processId-eq~"+updateForm.getProcessId());
//                examineF.add("customFormId-eq~"+updateForm.getCustomFormId());
                examineF.add("isEnd-eq~0");
                // 审批人为连级主管时
                if(updateForm.getExamineType() == 3){
                    examineF.add("conOrders-eq~"+ (updateForm.getConOrders() + 1));
                    examineWapper.setWhere(examineF, examineWrapper);
                    List list3 = super.list(examineWrapper);
                    if(list3.size() <= 0){
                        examineWrapper = Wrappers.query();
                        examineF = new ArrayList<>();
                        examineF.add("processApplyId-eq~"+updateForm.getProcessApplyId());
//                        examineF.add("applicantId-eq~"+updateForm.getApplicantId());
                        examineF.add("processId-eq~"+updateForm.getProcessId());
//                        examineF.add("customFormId-eq~"+updateForm.getCustomFormId());
                        examineF.add("isEnd-eq~0");
                        examineF.add("orders-eq~"+ (updateForm.getOrders() + 1));
                    }
                }else{
                    examineF.add("orders-eq~"+ (updateForm.getOrders() + 1));
                }
                examineWapper.setWhere(examineF, examineWrapper);
                list = super.list(examineWrapper);
                if(list.size() <= 0){
                    // 当前节点为最后一个节点 没下一节点时 所有节点流程结束
                    examineWrapper = Wrappers.query();
                    examineF = new ArrayList<>();
                    examineF.add("processApplyId-eq~"+updateForm.getProcessApplyId());
//                    examineF.add("applicantId-eq~"+updateForm.getApplicantId());
                    examineF.add("processId-eq~"+updateForm.getProcessId());
//                    examineF.add("customFormId-eq~"+updateForm.getCustomFormId());
                    examineF.add("isEnd-eq~0");
                    examineWapper.setWhere(examineF, examineWrapper);
                    list = super.list(examineWrapper);
                    for (ProcessExamine examineVO : list) {
                        examineVO.setIsEnd(1);
                        AssertUtil.isTrue(super.updateById(examineVO), "更新失败");
                    }
                    operationContent = content + "此节点设置为驳回继续流转至下一个审批节点,此节点为最后节点,此流程所有节点流程结束;";
                    // 用户申请审批状态改为拒绝
                    examine(updateForm.getProcessApplyId(), 9);
                }else{
                    // 下一节点审核状态从待流转改为待审批
                    for (ProcessExamine examineVO : list) {
                        examineVO.setResult(0);
                        examineVO.setReceiveTime(now);
                        AssertUtil.isTrue(super.updateById(examineVO), "更新失败");
                    }
                    operationContent = content + "此节点设置为驳回继续流转至下一个审批节点,审核将流转至下一节点;";
                }
            }
        }
        addForm.setOperationContent(operationContent);
        AssertUtil.isTrue(processRecordService.save(addForm), "创建失败");

        return HttpResult.success("审核成功");
    }

    /**
     * 通过/拒绝审批
     */
    @Transactional
    public HttpResult<Void> examine(Long processApplyId, Integer result) {
        ProcessApply processApply = processApplyService.getById(processApplyId);
        if (null != processApply){
            processApply.setResult(result);
            AssertUtil.isTrue(processApplyService.updateById(processApply), "更新失败");
        }
        if(processApply.getCustomFormId() != null){
//            if (collectWapper == null) {
//                collectWapper = new SqueryWapper<>(FormCollect.class);
//            }
//            QueryWrapper<FormCollect> collectWrapper = Wrappers.query();
//            List<String> f0 = new ArrayList<>();
//            f0.add("visibleRange-eq~0");
//            collectWapper.setWhere(f0, collectWrapper);
//            List<FormCollect> collectList = formCollectService.list(collectWrapper);
//            if(!collectList.isEmpty()){
//                String[] collectIds = new String[collectList.size()];
//                for (int i=0;i<collectList.size();i++) {
//                    collectIds[i] = String.valueOf(collectList.get(i).getId());
//                }
//            }
            FormCollect collect = formCollectService.getById(processApply.getCollectId());
            if(collect != null){
                if(collect.getVisibleRange() == 0){
                    Applicant applicant = applicantService.getById(processApply.getApplicantId());
//                    ClientUser user = clientUserService.getById(processApply.getApplicantId());
                    if(applicant != null){
                        if(result == 1){
                            applicant.setResult(2);
                        }else{
                            applicant.setResult(9);
                        }
                        AssertUtil.isTrue(applicantService.updateById(applicant), "更新失败");
                    }
                }else if(collect.getVisibleRange() == 1){
                    if (tenancyRoomWapper == null) {
                        tenancyRoomWapper = new SqueryWapper<>(TenancyRoom.class);
                    }
                    QueryWrapper<TenancyRoom> tenancyRoomWrapper = Wrappers.query();
                    List tenancyRoomF = new ArrayList<>();
                    tenancyRoomF.add("formResultId-eq~"+processApply.getCustomFormId());
                    tenancyRoomF.add("formApplyId-eq~"+processApply.getId());
                    tenancyRoomF.add("tenancyState-eq~0");
                    tenancyRoomF.add("applicantId-eq~"+processApply.getApplicantId());
                    tenancyRoomWapper.setWhere(tenancyRoomF, tenancyRoomWrapper);
                    List list = tenancyRoomService.list(tenancyRoomWrapper);
                    if(list.size() > 0){
                        TenancyRoom tenancyRoom = (TenancyRoom) list.get(0);
                        tenancyRoom.setTenancyState(1);
                        tenancyRoom.setExamineTime(LocalDateTime.now());
                        AssertUtil.isTrue(tenancyRoomService.updateById(tenancyRoom), "更新失败");

                        if(tenancyRoom.getTenancyWay() == 0){
                            Room room = roomService.getById(tenancyRoom.getRoomId());
                            if(room != null){
                                room.setUseState(1);
                                AssertUtil.isTrue(roomService.updateById(room), "更新失败");
                            }
                        }else{
                            Lease lease = leaseService.getById(tenancyRoom.getLeaseId());
                            if(lease != null){
                                lease.setUseState(1);
                                AssertUtil.isTrue(leaseService.updateById(lease), "更新失败");
                            }
                        }
                    }
                }
            }
        }else if(processApply.getTenancyRoomId() != null){
            Process process = processService.getById(processApply.getProcessId());
            if (null == process){
                return HttpResult.fail("B0000", I18n.build("流程应用为空！"));
            }
            if(process.getExamineType() == 2){
                tenancyRoomService.exitTenancy(processApply.getTenancyRoomId());
            } else if (process.getExamineType() == 4) {
                tenancyRoomService.sublet(processApply.getTenancyRoomId());
            }
        }
        return HttpResult.success();
    }

}
